package com.ruoyi.service.file.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.file.PFileInfo;
import com.ruoyi.domain.minio.MinioItem;
import com.ruoyi.mapper.file.PFileInfoMapper;
import com.ruoyi.service.file.PFileInfoService;
import com.ruoyi.utils.MinIoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州学院-计算机与信息工程学院
 * @ClassName PFileInfoServiceImpl
 * @Description TODO
 * @Date 2022/07/02 16:09
 */
@Service
public class PFileInfoServiceImpl implements PFileInfoService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Value("${minio.bucketName}")
    private String bucketName;
    @Resource
    private PFileInfoMapper fileInfoMapper;
    @Resource
    private MinIoUtil minIoUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long uploadFile(MultipartFile file) {
        // 判读是否上传成功

        System.out.println("shs" + file );


        String fileName = minIoUtil.upload(file, bucketName);

        System.out.println("__________" + fileName );
        if (StringUtils.isEmpty(fileName)) {
            logger.error("上传图片文件失败！！");
            throw new RuntimeException("上传图片文件失败");
        }

        /**
         * 添加信息到数据库表中
         * 并返回添加的id号
         */
        PFileInfo fileInfo = new PFileInfo(0L, bucketName, fileName,
                file.getSize(), file.getContentType(),new Date(), null);
        fileInfoMapper.uploadFile(fileInfo);
        return fileInfo.getFileId();

    }
}
